/*
空间——服务器基类
*/
package main

import (
    "../medusa"
    //"container/list"
)

type Space struct {
    space_uuid int32
    //client_list *list.List
    client_map map[int32]medusa.Acceptor
}

func NewSpace(space_uuid int32) *Space {
    space := new(Space)
    space.InitSpace(space_uuid)
    return space
}

func (self *Space) InitSpace(space_uuid int32) {

    //self.client_list = list.New() //list
    self.client_map = make(map[int32]medusa.Acceptor) //map
}

func (self *Space) GetUUID() int32 {
    return self.space_uuid
}

func (self *Space) Count() int32 {
    //return (int32)(self.client_list.Len()) //list
    return (int32)(len(self.client_map)) //map
}

func (self *Space) Into(acc medusa.Acceptor) {
    _, has := self.client_map[acc.GetUUID32()]
    if !has { //如果不存在，才需要添加
        //self.client_list.PushBack(client_uuid)     //list
        self.client_map[acc.GetUUID32()] = acc //map
    }

}

func (self *Space) Leave(client_uuid int32) {
    _, has := self.client_map[client_uuid]
    if has { //如果存在，才可以删除
        //list
        /*
        	el := self.client_list.Front()
        	for el != nil {
        		if el.Value.(medusa.Acceptor) == acc {
        			self.client_list.Remove(el)
        			break
        		}
        		el = el.Next()
        	}*/
        delete(self.client_map, client_uuid) //map
    }

}

func (self *Space) Clear() {
    //self.client_list = list.New() //list
    self.client_map = make(map[int32]medusa.Acceptor) //map
}

func (self *Space) Has(client_uuid int32) bool {
    _, has := self.client_map[client_uuid]
    return has
}

func (self *Space) Broadcast(pack *medusa.Packet) {
    except_uuid := pack.GetExceptUUID()
    //list广播
    /*
    	el := self.client_list.Front()
    	for el != nil {
    		client_uuid := el.Value.(int32)
    		if client_uuid != except_uuid {
    			Global().SendToClient(client_uuid, pack)
    		}
    		el = el.Next()
    	} //end : for...
    */
    for uuid, acc := range self.client_map {
        if uuid != except_uuid {
            acc.SendPacket(pack)
        }
    }

}

func (self *Space) CloseSpace() {

}
